/*
 * @lc app=leetcode.cn id=1380 lang=cpp
 *
 * [1380] 矩阵中的幸运数
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <cmath>

using namespace std;
// @lc code=start
class Solution {
public:
    vector<int> luckyNumbers (vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<int> res;
        for (int i = 0; i < m; i++) {
            // 找到这一行最小值的下标
            int maxColIndex = 0;
            for (int j = 1; j < n; j++) {
                if (matrix[i][j] < matrix[i][maxColIndex]) maxColIndex = j;
            }
            // 找到这一列最大值的下标
            int maxRowIndex = 0;
            for (int j = 1; j < m; j++) {
                if (matrix[j][maxColIndex] > matrix[maxRowIndex][maxColIndex]) maxRowIndex = j;
            }
            if (maxRowIndex == i) 
                res.push_back(matrix[maxRowIndex][maxColIndex]);
        }
        return res;
    }
};
// @lc code=end

